package com.kaver.feign.rpc.eureka;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kaver.feign.rpc.support.ApacheHttpClient;

import feign.Request;
import feign.Request.Options;
import feign.Response;

/**
 * @ClassName: EurekaApi
 * @Description: EurekaApi
 * @author kaver
 * @date 2019年5月29日 下午4:52:03
 * 
 */
public class EurekaApi {
	private static Logger log = LoggerFactory.getLogger(EurekaApi.class);
	
	private static ApacheHttpClient httpClient = new ApacheHttpClient(); 
	public static Map<String, Map<String, EurekaAppInstance>> getAllApp(String eurekaUrl) {
		Map<String, Map<String, EurekaAppInstance>> map = new HashMap<>();
		try {
			Map<String, Collection<String>> headers = new HashMap<>();
			headers.put("Accept", Arrays.asList("application/json"));
			Request request = Request.create("GET", eurekaUrl + "/eureka/apps", headers, null, Charset.defaultCharset());
			Options options = new Options(30000, 30000);
			Response response =  httpClient.execute(request, options);
			String json = response.body().toString();
			JSONObject jsonObject = JSON.parseObject(json);
			JSONObject applications = jsonObject.getJSONObject("applications");
			JSONArray array = applications.getJSONArray("application");
			if (array != null && !array.isEmpty()) {
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					String name = object.getString("name");
					JSONArray instance = object.getJSONArray("instance");
					if (instance != null && !instance.isEmpty()) {
						Map<String, EurekaAppInstance> appInstances = new HashMap<>();
						map.put(name, appInstances);
						for (int j = 0; j < instance.size(); j++) {
							JSONObject object2 = instance.getJSONObject(j);
							String instanceId = object2.getString("instanceId");
							String ipAddr = object2.getString("ipAddr");
							JSONObject port = object2.getJSONObject("port");
							String portStr = port.getString("$");
							EurekaAppInstance appInstance = new EurekaAppInstance();
							appInstance.setIp(ipAddr);
							appInstance.setPort(portStr);
							appInstances.put(instanceId, appInstance);
						}
					}
				}
			}
		} catch (IOException e) {
			log.error("getAllApp error", e);
			throw new RuntimeException(e);
		}
		return map;
	}

	public static Map<String, EurekaAppInstance> getAppInstance(String eurekaUrl, String appName) {
		Map<String, EurekaAppInstance> appInstances = new ConcurrentHashMap<>();
		try {
			Map<String, Collection<String>> headers = new HashMap<>();
			headers.put("Accept", Arrays.asList("application/json"));
			Request request = Request.create("GET", eurekaUrl + "/eureka/apps"+ appName, headers, null, Charset.defaultCharset());
			Options options = new Options(30000, 30000);
			Response response =  httpClient.execute(request, options);
			String json = response.body().toString();
			JSONObject jsonObject = JSON.parseObject(json);
			JSONObject application = jsonObject.getJSONObject("application");
			JSONArray instance = application.getJSONArray("instance");
			if (instance != null && !instance.isEmpty()) {
				for (int j = 0; j < instance.size(); j++) {
					JSONObject object2 = instance.getJSONObject(j);
					String instanceId = object2.getString("instanceId");
					String ipAddr = object2.getString("ipAddr");
					JSONObject port = object2.getJSONObject("port");
					String portStr = port.getString("$");
					EurekaAppInstance appInstance = new EurekaAppInstance();
					appInstance.setIp(ipAddr);
					appInstance.setPort(portStr);
					appInstances.put(instanceId, appInstance);
				}
			}
		} catch (IOException e) {
			log.error("getAppInstance error", e);
			throw new RuntimeException(e);
		}
		return appInstances;
	}
}
